package com.pktechan.manage.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.TradeOrder;
import com.pktechan.manage.entity.TradeOrderDetail;
import com.pktechan.manage.service.TradeOrderDetailService;
import com.pktechan.manage.service.TradeOrderService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.vo.TradeOrderDetailVO;
import com.pktechan.manage.vo.TradeOrderVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 订单相关功能
 * @author: s.zhang
 * @create: 2021-03-05 16:06
 **/
@Controller
@RequestMapping("tradeOrderController")
public class TradeOrderController {
    private static final Logger logger = LoggerFactory.getLogger(TradeOrderController.class);

    @Autowired
    private TradeOrderService tradeOrderService;
    @Autowired
    private TradeOrderDetailService tradeOrderDetailService;

    /**
     * 根据id查询订单表
     *
     * @param orderId
     * @param response
     */
    @RequestMapping(value = "getTradeOrderById", method = RequestMethod.POST)
    public void getTradeOrderById(@RequestParam Integer orderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + orderId + "查询状态流水");
            TradeOrder tradeOrder = tradeOrderService.getById(orderId);
            HtmlUtil.writerSuccessJson(response, tradeOrder);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 查询订单表列表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "getTradeOrderVoList", method = RequestMethod.POST)
    public void getTradeOrderVoList(@RequestBody TradeOrderVo tradeOrder, HttpServletResponse response) {
        try {
            logger.info("查询订单列表");
            PageHelper.startPage(tradeOrder.getPageNo(), tradeOrder.getPageSize());
            List<TradeOrderVo> list = tradeOrderService.getTradeOrderVoList(tradeOrder);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到订单表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "saveTradeOrder", method = RequestMethod.POST)
    public void saveTradeOrder(@RequestBody TradeOrder tradeOrder, HttpServletResponse response) {
        try {
            logger.info("添加到订单表中");
            tradeOrderService.save(tradeOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改订单表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "updateTradeOrder", method = RequestMethod.POST)
    public void updateTradeOrder(@RequestBody TradeOrder tradeOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + tradeOrder.getId() + "修改订单表");
            tradeOrderService.update(tradeOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id删除订单
     *
     * @param orderId
     * @param response
     */
    @RequestMapping(value = "deleteTradeOrderById", method = RequestMethod.POST)
    public void deleteTradeOrderById(@RequestParam Integer orderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + orderId + "删除订单信息");
            tradeOrderService.deleteById(orderId);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据id查询订单表详情
     *
     * @param orderDetailId
     * @param response
     */
    @RequestMapping(value = "getTradeOrderDetailById", method = RequestMethod.POST)
    public void getTradeOrderDetailById(@RequestParam Integer orderDetailId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + orderDetailId + "查询订单表详情");
            TradeOrderDetailVO tradeOrderDetail = tradeOrderDetailService.getTradeOrderDetailById(orderDetailId);
            HtmlUtil.writerSuccessJson(response, tradeOrderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据id查询订单表详情
     *
     * @param tradeOrderId
     * @param response
     */
    @RequestMapping(value = "getTradeOrderDetailByOrderId", method = RequestMethod.POST)
    public void getTradeOrderDetailByOrderId(@RequestParam Integer tradeOrderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + tradeOrderId + "查询订单表详情");
            List<TradeOrderDetailVO> tradeOrderDetailList = tradeOrderDetailService.getTradeOrderDetailByOrderId(tradeOrderId);
            HtmlUtil.writerSuccessJson(response, tradeOrderDetailList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 打开订单列表页面
     * @return
     */
    @RequestMapping("toTradeOrderVoList")
    public String toTradeOrderVoList() {
        return "tradeOrder/tradeOrderVoList";
    }
    /**
     * 打开订单详情页面
     * @return
     */
    @RequestMapping("toTradeOrderDetail")
    public String toTradeOrderDetail() {
        return "tradeOrder/tradeOrderDetail";
    }
}
